//https://leetcode.cn/problems/multiply-strings/

class Solution {
public:
    string multiply(string num1, string num2) {
        if (num1 == "0" || num2 == "0")
            return "0";

        int m = num1.size(), n = num2.size();
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());

        vector<int> temp(m + n - 1);

        //无进位相乘再相加
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                temp[i + j] += (num1[i] - '0') * (num2[j] - '0');

        //处理进位
        string ret;
        int cur = 0;
        int sum = 0;
        while (cur < m + n - 1 || sum)
        {
            if (cur < m + n - 1)
                sum += temp[cur++];
            ret += sum % 10 + '0';
            sum /= 10;
        }

        reverse(ret.begin(), ret.end());
        return ret;


    }
};